package thread;

import java.util.concurrent.*;

/**
 * 异步编排  测试
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-12
 */
public class CompletableFutureTest {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(4);
        // 开始时间
        long start = System.currentTimeMillis();
        // 任务1：睡眠1s
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1完成");
        }, executor);
        // 任务2：睡眠10s
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2完成");
        }, executor);
        // 任务3：睡眠5s
        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务3完成");
        }, executor);
        // 阻塞
//        CompletableFuture.allOf(future1,future2,future3).join();
        long end = System.currentTimeMillis();
        System.out.println("花费时间:" + (end - start));
//        executor.shutdown();
    }
}